define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fittings/order/index' + location.search,
                    add_url: 'fittings/order/add',
                    // edit_url: 'fittings/order/edit',
                    // del_url: 'fittings/order/del',
                    multi_url: 'fittings/order/multi',
                    import_url: 'fittings/order/import',
                    table: 'fittings_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sn', title: __('Sn'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"1":__('Pay_type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'contact', title: __('Contact'), operate: 'LIKE'},
                        {field: 'telephone', title: __('Telephone'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'deliver', title: __('Deliver'), searchList: {"1":__('Deliver 1')}, formatter: Table.api.formatter.normal},
                        {field: 'num', title: __('Num')},
                        {field: 'mark', title: __('Mark'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                        // {field: 'sale_status', title: __('Sale_status'), searchList: {"0":__('Sale_status 0'),"1":__('Sale_status 1'),"2":__('Sale_status 2')}, formatter: Table.api.formatter.status},
                        // {field: 'refund_status', title: __('Refund_status'), searchList: {"0":__('Refund_status 0'),"1":__('Refund_status 1'),"2":__('Refund_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'express', title: __('Express'),  formatter: Table.api.formatter.label,searchList:Config.express},
                        {field: 'express_sn', title: __('Express_sn'), operate: 'LIKE'},
                        // {field: 'refund_express', title: __('Refund_express'), operate: 'LIKE'},
                        // {field: 'refund_express_sn', title: __('Refund_express_sn'), operate: 'LIKE'},
                        // {field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'query',
                                title: '查看物流',
                                text: '查看物流',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'express/query?express_com={express}&express_no={express_sn}',//接口地址 控制器名/方法名
                                visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                    if (row.status >= '2') {
                                        return true;//显示
                                    } else {
                                        return false;//隐藏
                                    }

                                },
                            },
                            {
                                name: 'deliver',
                                title: '发货',
                                text: '发货',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'fittings/order/deliver',//接口地址 控制器名/方法名
                                visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                    if (row.status == '1') {
                                        return true;//显示
                                    } else {
                                        return false;//隐藏
                                    }
                                },
                            },
                            {
                                name: 'delete',
                                title: '删除',
                                icon: 'fa fa-trash',
                                text: '删除',
                                classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                url: 'fittings/order/del',//接口地址 控制器名/方法名
                                refresh: true,
                                visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                    if (row.status == '0'||row.status == '-1') {
                                        return true;//显示
                                    } else {
                                        return false;//隐藏
                                    }

                                },
                            },
                        ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deliver: function () {
            Controller.api.bindevent();
        },
        confirmreturn: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
